#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int

// ??????
uchar code seg_table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
// ?????
uchar hour=13, minute=59, second=55;
// ?????
uchar table[8];

sbit K1 = P3^0;  // ??K1??P3.0,????????


// ????,1ms
void delay1ms(uint i)
{
    uchar j;
    while(i--)
        for(j=0;j<125;j++);
}

// ???????
void display()
{
    uchar i;  // ???????????
    
    table[0]=seg_table[hour/10];
    table[1]=seg_table[hour%10];
    table[2]=0xbf;  // ?????,?":"
    table[3]=seg_table[minute/10];
    table[4]=seg_table[minute%10];
    table[5]=0xbf;  // ?????,?":"
    table[6]=seg_table[second/10];
    table[7]=seg_table[second%10];
    
    // ??????
    for(i=0;i<8;i++)
    {
        P1 = table[i];
        P2 = ~(1<<i);  // ????,??P2?
        delay1ms(1);
        P1 = 0x00;
        P2 = 0xff;
    }
}

// ???T1??????
void Timer1_ISR(void) interrupt 3
{
    TH1=(65536-50000)/256;
    TL1=(65536-50000)%256;
    second++;
    if(second>=60)
    {
        second=0;
        minute++;
        if(minute>=60)
        {
            minute=0;
            hour++;
            if(hour>=24)
            {
                hour=0;
            }
        }
    }
}

// ????1????
void Int1_ISR(void) interrupt 2
{
    hour=15;
    minute=45;
    second=58;
}

void main()
{
    // ??????T1???
    EA=1;       // ?????
    EX1=1;      // ??????1
    ET1=1;      // ?????T1??
    IT1=1;      // ??????1?????
    TH1=(65536-50000)/256;
    TL1=(65536-50000)%256;
    TMOD=0x10;  // T1????1,16????
    TR1=1;      // ?????1
    
    // ???
    while(1)
    {
        display();
        if(K1==0)
        {
            delay1ms(15);  // ??
            if(K1==0)
            {
                TR1=0;  // ????
                // ??????,????????1
                second++;
                if(second>=60)
                {
                    second=0;
                }
                TR1=1;  // ????
            }
            while(!K1);  // ??????
        }
    }
}